{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "9ac382de-ad26-486b-8b4c-2fe036c9c82c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "822407b0-ca66-4379-84ae-702ad4b0379f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train=pd.read_csv('train.csv')\n",
    "test=pd.read_csv('test.csv')\n",
    "datas = pd.concat([train, test], ignore_index = True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "defc67a8-9361-4635-a9f3-988ee920774d",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Fare']=datas['Fare'].fillna(7.8)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "44676038-3432-42c1-bb00-68add0746847",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Embarked'] = datas['Embarked'].fillna('C')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "f6b26aa3-f84b-48f6-a2e5-b569cee978fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Cabin']=datas['Cabin'].fillna(\"U\")\n",
    "datas['Cabin']=datas['Cabin'].str.get(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "3632a447-cd25-4f73-a245-49893ce92005",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "ages = datas[['Age', 'Pclass','Sex']]\n",
    "ages=pd.get_dummies(ages)\n",
    "known_ages = ages[ages.Age.notnull()].values\n",
    "unknown_ages = ages[ages.Age.isnull()].values\n",
    "y = known_ages[:, 0]\n",
    "X = known_ages[:, 1:]\n",
    "rfr = RandomForestRegressor(random_state=60, n_estimators=100, n_jobs=-1)\n",
    "rfr.fit(X, y)\n",
    "pre_ages = rfr.predict(unknown_ages[:, 1::])\n",
    "datas.loc[ (datas.Age.isnull()), 'Age' ] = pre_ages\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "c2097017-d19b-4983-adab-898f0807ae5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1309 entries, 0 to 1308\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  1309 non-null   int64  \n",
      " 1   Survived     891 non-null    float64\n",
      " 2   Pclass       1309 non-null   int64  \n",
      " 3   Name         1309 non-null   object \n",
      " 4   Sex          1309 non-null   object \n",
      " 5   Age          1309 non-null   float64\n",
      " 6   SibSp        1309 non-null   int64  \n",
      " 7   Parch        1309 non-null   int64  \n",
      " 8   Ticket       1309 non-null   object \n",
      " 9   Fare         1309 non-null   float64\n",
      " 10  Cabin        1309 non-null   object \n",
      " 11  Embarked     1309 non-null   object \n",
      "dtypes: float64(3), int64(4), object(5)\n",
      "memory usage: 122.8+ KB\n"
     ]
    }
   ],
   "source": [
    "datas.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "c2e9976d-7a41-47b9-a68a-862ade15acdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Title'] = datas['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())\n",
    "datas['Title'].replace(['Capt', 'Col', 'Major', 'Dr', 'Rev'],'Officer', inplace=True)\n",
    "datas['Title'].replace(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty', inplace=True)\n",
    "datas['Title'].replace(['Mme', 'Ms', 'Mrs'],'Mrs', inplace=True)\n",
    "datas['Title'].replace(['Mlle', 'Miss'], 'Miss', inplace=True)\n",
    "datas['Title'].replace(['Master','Jonkheer'],'Master', inplace=True)\n",
    "datas['Title'].replace(['Mr'], 'Mr', inplace=True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "00d3f16e-3e84-4dac-9a31-f3aaa09c25c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['Fam_size'] = datas['SibSp'] + datas['Parch'] + 1\n",
    "\n",
    "datas.loc[datas['Fam_size']>7,'Fam_type']=0\n",
    "datas.loc[(datas['Fam_size']>=2)&(datas['Fam_size']<=4),'Fam_type']=2\n",
    "datas.loc[(datas['Fam_size']>4)&(datas['Fam_size']<=7)|(datas['Fam_size']==1),'Fam_type']=1\n",
    "datas['Fam_type']=datas['Fam_type'].astype(np.int32)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "3a1c28b4-3557-42a3-a3f9-5ad53f25835b",
   "metadata": {},
   "outputs": [],
   "source": [
    "y=train['Survived']\n",
    "features = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\",\"Title\",\"Cabin\",\"Fam_size\",\"Embarked\"]\n",
    "# datas=datas.drop('Name',axis=1)\n",
    "# datas=datas.drop('Age',axis=1)\n",
    "# datas=datas.drop('Ticket',axis=1)\n",
    "# datas=datas.drop('Fam_type',axis=1)\n",
    "# datas=datas.drop('Fare',axis=1)\n",
    "# qq=pd.get_dummies(datas)\n",
    "train=datas[datas['Survived'].notnull()]\n",
    "test=datas[datas['Survived'].isnull()].drop('Survived',axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "9ddb565b-a415-4004-a4d5-c8271e46f5ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = pd.get_dummies(datas[features])\n",
    "X=X.loc[0:890]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "a7cf396d-5af9-4d32-9967-32b8f9bb1e69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8742985409652076"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型初步训练 \n",
    "from xgboost import XGBClassifier \n",
    "xg = XGBClassifier() \n",
    "\n",
    "xg.fit(X, y) \n",
    "xg.score(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "ed2b57ea-8223-43dc-a9d0-f5f232416335",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test = pd.get_dummies(datas[features])\n",
    "X_test = X_test.loc[891:1308]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "187d6e3b-e8a5-4225-a2e1-530879c3763c",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = xg.predict(X_test)\n",
    "\n",
    "output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions.astype(int)})\n",
    "output.to_csv('mypredictxg.csv', index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "488d08c6-f9d6-4352-88b1-c488f372c8b0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43a6abb3-2fbb-47d0-b44d-057a41a1bb2f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b67ee00-b2cb-4ef7-ae46-cc33fef5c55c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
